package cn.kgc.springbootdemo.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.img.Img;
import cn.hutool.core.io.FileUtil;
import cn.kgc.springbootdemo.annotation.PassToken;
import cn.kgc.springbootdemo.annotation.PreAuthorize;
import cn.kgc.springbootdemo.util.R;
import cn.kgc.springbootdemo.util.RedisUtil;
import cn.kgc.springbootdemo.util.RequestIpUtils;
import cn.kgc.springbootdemo.util.TokenUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/test")
@Api("测试控制器")
@Slf4j
public class TestController {


    private TokenUtil tokenUtil;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RedisUtil redis;


    @GetMapping("/secretKey")
    @PassToken
    public void secretKey(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        //将验证码绑定session
        request.getSession().setAttribute("VerifyCode", lineCaptcha.getCode());
        response.setContentType("image/jpeg");
        response.setHeader("Content-Disposition","attachment;filename=test.png");
        //设置响应头
        response.setHeader("Pragma", "no-cache");
        //设置响应头
        response.setHeader("Cache-Control", "no-cache");
        //在代理服务器端防止缓冲
        response.setDateHeader("Expires", 0);
        //输出
        lineCaptcha.write(response.getOutputStream());
        response.getOutputStream().flush();
    }

    public void shangchuan(){
        Img.from(FileUtil.file("C:/Users/开拓未来-研发部-java开发-Dzj/Desktop/C户型.jpg"))
                .setQuality(0.8)//压缩比率
                .write(FileUtil.file("C:/Users/开拓未来-研发部-java开发-Dzj/Desktop//压缩图片.jpg"));
    }



    @ApiOperation("测试-hello")
    @GetMapping(value = "/hello")
    public R<String> holle(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return R.ok(RequestIpUtils.getIpAddr());
    }

    @GetMapping("/getIp")
    @PassToken
    public R getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        System.out.println("x-forwarded-for ip: " + ip);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            System.out.println("Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            System.out.println("WL-Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            System.out.println("HTTP_CLIENT_IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
            System.out.println("X-Real-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            System.out.println("getRemoteAddr ip: " + ip);
        }
        System.out.println("获取客户端ip: " + ip);
        return R.ok(ip);
    }


    @GetMapping
    public R<String> test(){
        return R.ok();
    }

//    @GetMapping("/getMessage")
//    public R<String> getMessage() throws InterruptedException {
//        log.info("当前登录用户{}", tokenUtil.getUser());
//        return R.ok("通过验证");
//    }


//    @PostMapping("/sendDirectMessage")
//    @PassToken
//    public R<PageResult<UserMongo>> sendDirectMessage(@Valid @RequestBody MongoPageUtils mongoPageUtils){
//        Query query=new Query(Criteria.where((MongodbColumUtils.getEntityClum(UserMongo::getAge)UserMongo.Alias.age).gte(80));
//        return R.ok(mongoPageUtils.pageQuery(query, UserMongo.class, mongoTemplate));
//    }

}
